﻿package{
	import flash.display.Sprite;
	import flash.display.Shape;
	import flash.events.*;
	import flash.geom.Point;
	import flash.display.Bitmap;
	import flash.filters.BlurFilter;
	import flash.xml.*;
	import LearnSprite;
	import RightOrWrongButton;
	public class characterFrame extends Sprite{
		private var mainSprite:Sprite;
		private var miZiGe:Shape;
		private var hintButton:myButton;
		private var hintButtonSprite:Sprite;
		private var states:String;
		private var character:ChineseCharacter;
		private var flag:Boolean;//用来标识写动作的标志位
		private var drawShape:Shape;
		private var hintSprite:Sprite;
		private var pointsArray:Array;
		private var strokes:Array;
		private var strokeShapeForWrite:Array;
		private var strokeShapeForFill:Array;
		private var strokeHasBeenWritten:Array;
		private var writeStroke:stroke;
		private var startAndEndPoints:Array;
		private var moveShape:Array;
		private var addShapes:Array;
		private var nextStroke:int;
		private var learnSprite:LearnSprite;
		private var rightBorderShape:Shape;
		private var wrongBorderShape:Shape;
		private var showFilterCount:Number;
		private var characterFilterRight:Bitmap;
		private var characterFilterWrong:Bitmap;
		private var filterSprite:Sprite;
		private var style:XMLList;
		private var cfWidth:Number;
		private var hss:Number;//hintSpriteScale
		//private var writtenIsOk:writtenIsOk;
		private var isRight:Boolean;
		private var repeatButton:myButton;
		private var rightOrWrongButton:RightOrWrongButton;
		private var rightOrWrongButtonSprite:Sprite;
		private var drawShapeSprite:Sprite;
		private var errorWritten:int;
		private var mclkFlag:Boolean;
		public function characterFrame(cc:ChineseCharacter,stl:XMLList){
				//this.width=302;
				//this.height=302;
				//this.writtenIsOk=new writtenIsOk();
				//this.width=350;
				//this.height=350;
				this.errorWritten=0;
				this.isRight=false;
				this.style=stl;
				this.rightOrWrongButtonSprite=new Sprite();
				//this.rightOrWrongButtonSprite.graphics.lineStyle(1,0x000000);
				//this.rightOrWrongButtonSprite.graphics.beginFill(0xcccccc);
				//this.rightOrWrongButtonSprite.graphics.drawRoundRect(0,0,74,34,15,15);
				//this.rightOrWrongButtonSprite.graphics.endFill();
				this.repeatButton=new myButton("Repeat",Number(style.characterFrame.repeatButton.width),Number(style.characterFrame.repeatButton.height));
				this.rightOrWrongButton=new RightOrWrongButton(style.characterFrame.rightOrWrongButton);
				this.rightOrWrongButton.addEventListener(MouseEvent.CLICK,changeBorder,false,2);
				this.repeatButton.visible=false;
				this.repeatButton.x=1;
				this.repeatButton.y=1;
				this.repeatButton.addEventListener(MouseEvent.CLICK,repeat,true,2);
				this.rightOrWrongButton.x=1;
				this.rightOrWrongButton.y=1;
				this.rightOrWrongButtonSprite.addChild(rightOrWrongButton);
				this.rightOrWrongButtonSprite.x=style.characterFrame.learnButton.x;
				this.rightOrWrongButtonSprite.y=style.characterFrame.learnButton.y;
				this.rightOrWrongButtonSprite.visible=false;
				this.addChild(rightOrWrongButtonSprite);
				this.cfWidth=Number(style.characterFrame.width);
				this.hss=Number(style.characterFrame.hintSprite.inNormalCharacterFrame.scale);
				this.showFilterCount=0;
				this.drawShapeSprite=new Sprite();
				
				this.filterSprite=new Sprite();
				this.rightBorderShape=new Shape();
				rightBorderShape.x=0;
				rightBorderShape.y=0;
				rightBorderShape.graphics.lineStyle(style.characterFrame.borderShape.right.width,style.characterFrame.borderShape.right.color);
				rightBorderShape.graphics.moveTo(25,Number(style.characterFrame.borderShape.right.width)/2-1);				
				rightBorderShape.graphics.lineTo(cfWidth-25,Number(style.characterFrame.borderShape.right.width)/2-1);
				rightBorderShape.graphics.curveTo(cfWidth,0,cfWidth-Number(style.characterFrame.borderShape.right.width)/2+1,25);
				rightBorderShape.graphics.lineTo(cfWidth-Number(style.characterFrame.borderShape.right.width)/2+1,cfWidth-25);
				rightBorderShape.graphics.curveTo(cfWidth,cfWidth,cfWidth-25,cfWidth-Number(style.characterFrame.borderShape.right.width)/2+1);
				rightBorderShape.graphics.lineTo(25,cfWidth-Number(style.characterFrame.borderShape.right.width)/2+1);
				rightBorderShape.graphics.curveTo(0,cfWidth,Number(style.characterFrame.borderShape.right.width)/2-1,cfWidth-25);
				rightBorderShape.graphics.lineTo(Number(style.characterFrame.borderShape.right.width)/2-1,25);
				rightBorderShape.graphics.curveTo(0,0,25,Number(style.characterFrame.borderShape.right.width)/2-1);
				
				rightBorderShape.visible=false;
				this.wrongBorderShape=new Shape();
				wrongBorderShape.x=0;
				wrongBorderShape.y=0;
				wrongBorderShape.graphics.lineStyle(style.characterFrame.borderShape.wrong.width,style.characterFrame.borderShape.wrong.color);
				wrongBorderShape.graphics.moveTo(25,Number(style.characterFrame.borderShape.wrong.width)/2-1);				
				wrongBorderShape.graphics.lineTo(cfWidth-25,Number(style.characterFrame.borderShape.wrong.width)/2-1);
				wrongBorderShape.graphics.curveTo(cfWidth,0,cfWidth-Number(style.characterFrame.borderShape.wrong.width)/2+1,25);
				wrongBorderShape.graphics.lineTo(cfWidth-Number(style.characterFrame.borderShape.wrong.width)/2+1,cfWidth-25);
				wrongBorderShape.graphics.curveTo(cfWidth,cfWidth,cfWidth-25,cfWidth-Number(style.characterFrame.borderShape.wrong.width)/2+1);
				wrongBorderShape.graphics.lineTo(25,cfWidth-Number(style.characterFrame.borderShape.wrong.width)/2+1);
				wrongBorderShape.graphics.curveTo(0,cfWidth,Number(style.characterFrame.borderShape.wrong.width)/2-1,cfWidth-25);
				wrongBorderShape.graphics.lineTo(Number(style.characterFrame.borderShape.wrong.width)/2-1,25);
				wrongBorderShape.graphics.curveTo(0,0,25,Number(style.characterFrame.borderShape.wrong.width)/2-1);
				
				wrongBorderShape.visible=false;
				this.mainSprite=new Sprite();
				this.learnSprite=new LearnSprite(cc,style);
				this.hintButton=new myButton("Hint",Number(style.characterFrame.hintButton.width),Number(style.characterFrame.hintButton.height));
				hintButton.x=1;
				hintButton.y=1;
				hintButton.scaleX=1;
				hintButton.scaleY=1;
				this.hintButton.addEventListener(MouseEvent.CLICK,showHint);
				this.hintButtonSprite=new Sprite();
				this.hintButtonSprite.x=0;
				this.hintButtonSprite.y=0;				
				this.hintButtonSprite.graphics.lineStyle(1,0x000000);
				this.hintButtonSprite.graphics.beginFill(0xcccccc);
				this.hintButtonSprite.graphics.drawRoundRect(0,0,Number(style.characterFrame.hintButton.width)+2,Number(style.characterFrame.hintButton.height)+2,15,15);
				this.hintButtonSprite.graphics.endFill();
				this.hintButtonSprite.x=style.characterFrame.hintButton.x;
				this.hintButtonSprite.y=style.characterFrame.hintButton.y;
				this.mainSprite.addChild(this.hintButtonSprite);
				this.hintButtonSprite.addChild(this.repeatButton);
				this.hintButtonSprite.addChild(this.hintButton);
				this.learnSprite.x=style.characterFrame.learnButton.x;
				this.learnSprite.y=style.characterFrame.learnButton.y;
				this.learnSprite.scaleX=1;
				this.learnSprite.scaleY=1;
				this.mainSprite.addChild(this.learnSprite);
				this.mainSprite.name="ms";
				this.mainSprite.x=0;
				this.mainSprite.y=0;
				this.addShapes=new Array();
				//this.mainSprite.width=300;
				//this.mainSprite.height=300;
				this.moveShape=new Array();
				this.nextStroke=0;
				this.character=cc;
				this.strokes=cc.getStrokes();
				this.characterFilterRight=cc.getCharacterFilterRight();
				characterFilterRight.x=0;
				characterFilterRight.y=0;
				this.filterSprite.addChild(characterFilterRight);
				this.characterFilterWrong=cc.getCharacterFilterWrong();
				characterFilterWrong.x=0;
				characterFilterWrong.y=0;
				this.filterSprite.addChild(characterFilterWrong);
			
				this.filterSprite.scaleX=hss;
				this.filterSprite.scaleY=hss;
				this.filterSprite.x=(cfWidth-350*hss)/2;
				this.filterSprite.y=(cfWidth-350*hss)/2;
				
				this.mainSprite.addChild(filterSprite);
				this.filterSprite.visible=false;
				this.strokeShapeForWrite=cc.getStrokeShapeForWrite();
				this.strokeShapeForFill=cc.getStrokeShapeForFill();
				this.strokeHasBeenWritten=new Array();
				for(var k=0;k<strokes.length;k++){
					var tem=["0","0"];
					this.strokeHasBeenWritten.push(tem);
					}
				this.startAndEndPoints=new Array();
				this.startAndEndPoints=cc.getStartAndEndPoints();
				this.flag=false;
				this.drawShape=new Shape();
				this.drawShape.x=0;
				this.drawShape.y=0;
				this.states="all";
				graphics.lineStyle(style.characterFrame.graphics.lineWidth,style.characterFrame.graphics.color);
				graphics.beginFill(0xffffff);
				graphics.drawRoundRect(0,0,cfWidth,cfWidth,50,50);
				graphics.endFill();
				this.pointsArray=new Array();
				this.miZiGe=new Shape();
				/*this.miZiGe.width=302;
				this.miZiGe.height=302;
				this.miZiGe.scaleX=1;
				this.miZiGe.scaleY=1;
				*/
				this.mainSprite.addChild(this.drawShapeSprite);
				this.miZiGe.name="mzg";
				miZiGe.graphics.lineStyle(1,0x000000);
				miZiGe.graphics.beginFill(0xffffff);
				miZiGe.graphics.drawRoundRect(0,0,cfWidth,cfWidth,50,50);
				miZiGe.graphics.endFill();
				miZiGe.graphics.lineStyle(1,0xcccccc,1);
				miZiGe.graphics.moveTo(10,10);
				miZiGe.graphics.lineTo(cfWidth-10,cfWidth-10);

				miZiGe.graphics.moveTo(cfWidth-10,10);
				miZiGe.graphics.lineTo(10,cfWidth-10);

				miZiGe.graphics.moveTo(cfWidth-10,10);
				miZiGe.graphics.lineTo(10,cfWidth-10);

				miZiGe.graphics.moveTo(cfWidth/2,1);
				miZiGe.graphics.lineTo(cfWidth/2,cfWidth);

				miZiGe.graphics.moveTo(1,cfWidth/2);
				miZiGe.graphics.lineTo(cfWidth,cfWidth/2);
				miZiGe.x=0;
				miZiGe.y=0;
				this.mainSprite.addChild(miZiGe);
				
				
				this.drawShapeSprite.addChild(drawShape);
				//this.mainSprite.scaleX=1;
				//this.mainSprite.scaleY=1;
				this.hintSprite=new Sprite();
				var cs=this.character.getFullShape();
				cs.x=0;
				cs.y=0;
				
				hintSprite.addChild(cs);
				hintSprite.x=(cfWidth-350*hss)/2;
				hintSprite.y=(cfWidth-350*hss)/2;
				//trace(hintSprite.x);
				hintSprite.scaleX=hss;
				hintSprite.scaleY=hss;
				hintSprite.alpha=0;
				addChild(hintSprite);
				
				addChild(mainSprite);
				this.addChild(rightBorderShape);
				this.addChild(wrongBorderShape);				
				//this.mainSprite.setChildIndex(this.borderShape,0);
				this.mainSprite.setChildIndex(this.learnSprite,0);
				this.mainSprite.setChildIndex(this.filterSprite,2);
				this.mainSprite.setChildIndex(this.hintButtonSprite,1);
				this.mainSprite.setChildIndex(miZiGe,2);
				this.mainSprite.setChildIndex(drawShapeSprite,3);
				this.setChildIndex(mainSprite,1);
				this.setChildIndex(hintSprite,3);
				this.addEventListener(MouseEvent.MOUSE_DOWN,mDown);
				this.addEventListener(MouseEvent.MOUSE_UP,mUp);
				this.addEventListener(MouseEvent.MOUSE_OUT,mOut);
				this.drawShapeSprite.addEventListener(Event.ENTER_FRAME,mMove);
				this.addEventListener(MouseEvent.CLICK,mclk);
				this.mclkFlag=true;
			}
		function mclk(e:MouseEvent){
			trace("clicked/"+mclkFlag+this.done());
			e.stopImmediatePropagation();
			if(this.done()==true){
				this.mclkFlag=false;
				this.removeEventListener(MouseEvent.CLICK,mclk);
				this.addEventListener(MouseEvent.CLICK,mclk2);
				}
			}
		function mclk2(e:MouseEvent){
			//trace("clicked");
			//e.stopImmediatePropagation();
			
			}
		function changeBorder(e:MouseEvent){
			e.stopImmediatePropagation();
			trace(this.isRight);
			if(this.isRight==false){
				this.isRight=true;
				this.rightBorderShape.visible=true;
				this.wrongBorderShape.visible=false;
				this.rightOrWrongButton.right();
				}
			else{
				this.isRight=false;
				this.rightBorderShape.visible=false;
				this.wrongBorderShape.visible=true;
				this.rightOrWrongButton.wrong();
				}
			
			}
		function mDown(e:MouseEvent){
				//trace("down");
				//trace(mouseX+"/"+mouseY);
				//trace(this.miZiGe.hitTestPoint(stage.mouseX,stage.mouseY));
			
				e.stopImmediatePropagation();
				this.drawShape.graphics.lineStyle(style.characterFrame.draw.lineWidth,style.characterFrame.draw.color);
				this.drawShape.graphics.moveTo(mouseX,mouseY);
				this.flag=true;
			
				//this.addEventListener(MouseEvent.MOUSE_UP,mUp);

			}
		function mOut(e:MouseEvent){
			//slush();
			}
		function mUp(e:MouseEvent){
				e.stopPropagation();
				this.flag=false;
				//this.drawShapeSprite.removeEventListener(MouseEvent.MOUSE_MOVE,mMove);
				//this.removeEventListener(MouseEvent.MOUSE_MOVE,mUp);
				this.drawShape.graphics.clear();
				
				if(this.pointsArray.length>5){
				trace(pointsArray.length);
				 writeStroke=new stroke(pointsArray);
				var stk=writeStroke.getStroke();
				//trace("stk:"+stk);
				var wse=writeStroke.getSE();
				var isAccept=0;
				var sameIsAcc=0;
				var sameStrokeArray:Array=new Array();
				for(var k =0;k<this.strokes.length;k++){
					
					if(stk[0]==this.strokes[k]&&this.strokeHasBeenWritten[k][0]==0){
						var se=startAndEndPoints[k];
						//trace(se);
						var len=Math.sqrt((se[0].x-se[1].x)*(se[0].x-se[1].x)+(se[0].y-se[1].y)*(se[0].y-se[1].y));//笔画长度
						var cjs=Math.sqrt((se[0].x-wse[0].x)*(se[0].x-wse[0].x)+(se[0].y-wse[0].y)*(se[0].y-wse[0].y));//容错长度x
						var cje=Math.sqrt((se[1].x-wse[1].x)*(se[1].x-wse[1].x)+(se[1].y-wse[1].y)*(se[1].y-wse[1].y));//容错长度y
						//trace(cjs+"//"+cje);
						if(cjs<=100&&cje<=100){
							this.strokeHasBeenWritten[k][0]=1;
							var currentShape=this.strokeShapeForWrite[k];
							currentShape[0].x=wse[0].x-se[0].x*hss;
							currentShape[0].y=wse[0].y-se[0].y*hss;
							currentShape[0].name=k;
							currentShape[0].scaleX=hss;
							currentShape[0].scaleY=hss;
							this.mainSprite.addChild(currentShape[0]);
							this.moveShape.push(currentShape[0]);
							this.addShapes.push(k);
							if(this.nextStroke!=k){
								
								}
							else if(this.nextStroke==this.strokes.length){
								this.nextStroke=0;
								}
							else{
								this.nextStroke++;
								}
							for(var k1=0;k1<sameStrokeArray.length;k1++){
								this.strokeHasBeenWritten[sameStrokeArray[k1]][1]--;
								}
								
							isAccept=2;//笔画被接受
							sameIsAcc=2;//笔画被接受，不存在相同笔画间的重复计算
							break;
							}
						else{
							this.strokeHasBeenWritten[k][1]++;
							//this.errorWritten++;
							sameIsAcc=1;
							
							sameStrokeArray.push(k);
							}
						}
					else if(stk[0]==this.strokes[k]&& this.strokeHasBeenWritten[k][0]==1){
						sameIsAcc=1;
						}
					else if(k==(this.strokes.length-1)){
						for( k1=1;k1<sameStrokeArray.length;k1++){
								this.strokeHasBeenWritten[sameStrokeArray[k1]][1]--;
								}
						//sameIsAcc=0;
						if(sameIsAcc==0){
								isAccept=1;
							}
						if(sameIsAcc==1&&sameStrokeArray.length==0){
							isAccept=1;
							sameIsAcc=0;
							}
						}
					}
				if(isAccept==0){
					
					}
				else if(isAccept==1){ 
					
					this.strokeHasBeenWritten[this.nextStroke][1]++;
					//trace("111"+nextStroke);
					this.errorWritten++;
					}
				if(sameIsAcc==1){
					//trace("111111");
					this.errorWritten++;
					}
				else{
					
					}
				
				
				}
				trace(this.errorWritten);
				if(rightOrWrong()==false){
					this.wrongBorderShape.visible=true;
					this.rightBorderShape.visible=false;
					}
				
				this.pointsArray=new Array();
				if(done()==true){
					//trace(strokeHasBeenWritten);
					this.repeatButton.visible=true;
					this.rightOrWrongButtonSprite.visible=true;
					this.hintButton.visible=false;
					this.learnSprite.visible=false;
					this.learnSprite.letlearnSpriteHide();
					this.filterSprite.addEventListener(Event.ENTER_FRAME,showFilter);
					this.nextStroke=0;
					if(rightOrWrong()==true){
						this.rightBorderShape.visible=true;
						this.wrongBorderShape.visible=false;
						this.rightOrWrongButton.right();
					}
					else{
						this.rightBorderShape.visible=false;
						this.wrongBorderShape.visible=true;
						this.rightOrWrongButton.wrong();
						}
					this.removeEventListener(MouseEvent.MOUSE_DOWN,mDown);
					//this.addEventListener(MouseEvent.MOUSE_DOWN,md);
					this.removeEventListener(MouseEvent.MOUSE_UP,mUp);
					}
				if(!this.hasEventListener(Event.ENTER_FRAME)){
					this.addEventListener(Event.ENTER_FRAME,shapeMovingOn);
				}
			}
		function md(e:MouseEvent){
			e.stopImmediatePropagation();
				trace("down");
				//trace(mouseX+"/"+mouseY);
				//trace(this.miZiGe.hitTestPoint(stage.mouseX,stage.mouseY));
				//
				

			}
		function shapeMovingOn(e:Event){
				for(var k=0;k<this.moveShape.length;k++){
					if(Math.abs(this.moveShape[k].x-(cfWidth-hss*350)/2)<=1&&Math.abs(this.moveShape[k].y-(cfWidth-hss*350)/2)<=1){
						moveShape.splice(k,1);
						break;
						}
					else{
						this.moveShape[k].x=this.moveShape[k].x-(this.moveShape[k].x-(cfWidth-hss*350)/2)*0.1;//以缓动方式移动
						this.moveShape[k].y=this.moveShape[k].y-(this.moveShape[k].y-(cfWidth-hss*350)/2)*0.1;
						}
					}
			}
		public function slush(){
			this.removeEventListener(MouseEvent.MOUSE_MOVE,mMove);
					this.removeEventListener(MouseEvent.MOUSE_MOVE,mUp);
					this.pointsArray=new Array();
					this.drawShape.graphics.clear();
			
			}
		function mMove(e:Event){
			e.stopPropagation();
				//trace(mouseX+"/"+mouseY);
				
				
				if(mouseX<5||mouseX>(cfWidth-5)||mouseY<5||mouseY>(cfWidth-5)){
					this.flag=false;
					this.removeEventListener(MouseEvent.MOUSE_MOVE,mMove);
					this.removeEventListener(MouseEvent.MOUSE_MOVE,mUp);
					this.pointsArray=new Array();
					this.drawShape.graphics.clear();
					}
				if(this.flag==true&&this.states=="all"){
					this.drawShape.graphics.lineTo(mouseX,mouseY);
					this.pointsArray.push(mouseX+","+mouseY);
					}
			
			}
		
		public function showRect(x:Number,y:Number){
				this.states="rect";
				this.scaleX=x;
				this.scaleY=y;
				if(this.hintSprite.hasEventListener(Event.ENTER_FRAME)){
					this.hintSprite.removeEventListener(Event.ENTER_FRAME,hint);
					}
				if(this.mclkFlag==true){
					this.removeEventListener(MouseEvent.CLICK,mclk);
					this.addEventListener(MouseEvent.CLICK,mclk2);
					this.mclkFlag=false;
				}
				this.rightOrWrongButtonSprite.visible=false;
				//this.drawShape.visible=false;
				//this.miZiGe.visible=false;
				//this.y=this.y+(1-y)*this.height/2;
				this.mainSprite.visible=false;
				this.hintSprite.alpha=0;
				if(done()==true){
					//this.borderShape.visible=true;
					this.hintSprite.alpha=1;
					this.hintSprite.scaleX=style.characterFrame.hintSprite.inSmallCharacterFrame.scale;
					this.hintSprite.scaleY=style.characterFrame.hintSprite.inSmallCharacterFrame.scale;
					hintSprite.x=(cfWidth-Number(style.characterFrame.hintSprite.inSmallCharacterFrame.scale)*350)/2;
					hintSprite.y=(cfWidth-Number(style.characterFrame.hintSprite.inSmallCharacterFrame.scale)*350)/2;
					}
			}
		public function showAll(x,y){
				this.states="all";
				//this.drawShape.visible=true;
				this.rightOrWrongButtonSprite.visible=true;
				//trace(this.hasEventListener(MouseEvent.MOUSE_DOWN));
				if(this.mclkFlag==false&&this.done()==false){
					this.removeEventListener(MouseEvent.CLICK,mclk2);
					this.addEventListener(MouseEvent.CLICK,mclk,false,2);
					this.mclkFlag=true;
					trace("all");
					}
					
						//this.addEventListener(MouseEvent.CLICK,mclk);
				
				this.hintSprite.scaleX=hss;
				this.hintSprite.scaleY=hss;
				hintSprite.x=(cfWidth-hss*350)/2;
				hintSprite.y=(cfWidth-hss*350)/2;
				//trace(hintSprite.x+"/"+hss);
				this.scaleX=1;
				this.scaleY=1;
				this.x=x;
				this.y=y;
				this.mainSprite.visible=true;
			}
			
		public function showHint(e:MouseEvent){
				
				if(this.hintButton.getClicked()==true){
					this.hintButton.enableStyleChange();
					this.hintSprite.alpha=0.6;
					if(this.hintSprite.hasEventListener(Event.ENTER_FRAME)){
					
						}
					else{
						this.hintSprite.addEventListener(Event.ENTER_FRAME,hint);
		
						}
					}
				else{
					this.hintButton.disableStyleChange();
					this.hintSprite.alpha=0.6;
					if(this.hintSprite.hasEventListener(Event.ENTER_FRAME)){
						this.hintSprite.removeEventListener(Event.ENTER_FRAME,hint);
						}
					
				}
		}
		function hint(e:Event){
				if(this.hintSprite.alpha>0){
					this.hintSprite.alpha-=style.characterFrame.hintSprite.fadeSpeed;
					
					}
			}
		public function eraser(){
			if(this.addShapes.length>0&&done()==false){
				var k=this.addShapes[this.addShapes.length-1];
				if(this.mainSprite.getChildByName(k)){
					this.mainSprite.removeChild(this.mainSprite.getChildByName(k));
				}
				this.strokeHasBeenWritten[k][0]=0;
				this.strokeHasBeenWritten[k][1]=0;
				//trace(nextStroke+"/"+k);
				//trace(addShapes.join("//"));
				if(k+1==nextStroke){
					nextStroke=nextStroke-1;
					trace(nextStroke);
					}
				this.addShapes.pop();
			}
			else if(this.addShapes.length==this.strokes.length&&done()==true){
				this.rightBorderShape.visible=false;
				this.wrongBorderShape.visible=false;
				this.errorWritten=0;
				this.filterSprite.visible=false;
				this.learnSprite.visible=true;
				this.hintButton.visible=true;
				this.repeatButton.visible=false;
				this.rightOrWrongButtonSprite.visible=false;
				this.filterSprite.removeEventListener(Event.ENTER_FRAME,showFilter);
				for(var k1=0;k1<this.strokes.length;k1++){
					
					if(this.mainSprite.getChildByName(k1)){
						this.mainSprite.removeChild(this.mainSprite.getChildByName(k1));
					}
					this.strokeHasBeenWritten[k1]=0;
					}
					
				
					
				this.nextStroke=0;
						
				this.addShapes=new Array();
				this.addEventListener(MouseEvent.MOUSE_DOWN,mDown);
				//this.addEventListener(MouseEvent.MOUSE_UP,mUp);
				}
			}
		public function getStates():String{
				return this.states;
			}
		private function rightOrWrong():Boolean{
			var countError=0;
			trace(strokeHasBeenWritten);
			for(var k=0;k<strokeHasBeenWritten.length;k++){
				if(strokeHasBeenWritten[k][1]>=style.characterFrame.errorCount.eachStroke){
					countError++;
					}
				else{
					continue;
					}
				}
			trace(countError+"/"+style.characterFrame.errorCount.totle);
			
			if(countError>=style.characterFrame.errorCount.totle){
				this.isRight=false;
				}
			else{
				this.isRight=true;
				}
			
			return this.isRight;
			
			}
		public function done():Boolean{
			for(var k=0;k<strokeHasBeenWritten.length;k++){
				if(strokeHasBeenWritten[k][0]==0){
					return false;
					}
				else if(strokeHasBeenWritten[k][0]==1){
					continue;
				}
				}
			return true;
			}
		function showFilter(eve:Event):void {
			if(this.moveShape.length==0){
				var characterFilter;
				this.filterSprite.visible=true;
				if(this.isRight==true){
					this.characterFilterRight.visible=true;
					this.characterFilterWrong.visible=false;
					characterFilter=characterFilterRight;
					}
				else{
					this.characterFilterRight.visible=false;
					this.characterFilterWrong.visible=true;
					characterFilter=characterFilterWrong;
					}
				showFilterCount=showFilterCount%16+0.02;
				
				var c=Math.abs(Math.sin(showFilterCount)*12);


			

				var myBlurfilter:BlurFilter=new BlurFilter(c,c,3);
				var filterArray:Array = new Array();

				filterArray.push(myBlurfilter);

				characterFilter.filters=filterArray;
			}


		}
		function repeat(e:MouseEvent){
			e.stopImmediatePropagation();
			init();
			}
		public function init(){
				this.learnSprite.letlearnSpriteHide();
				this.rightBorderShape.visible=false;
				this.wrongBorderShape.visible=false;
				this.errorWritten=0;
				this.filterSprite.visible=false;
				this.learnSprite.visible=true;
				this.hintSprite.alpha=0;
				this.hintButton.resetClicked();
				this.hintButton.enableStyleChange();
				this.hintButton.visible=true;
				this.repeatButton.visible=false;
				this.rightOrWrongButtonSprite.visible=false;
				if(this.filterSprite.hasEventListener(Event.ENTER_FRAME)){
					this.filterSprite.removeEventListener(Event.ENTER_FRAME,showFilter);
				}
				for(var k1=0;k1<this.strokes.length;k1++){
					
					if(this.mainSprite.getChildByName(k1)){
						this.mainSprite.removeChild(this.mainSprite.getChildByName(k1));
					}
					this.strokeHasBeenWritten[k1][0]=0;
					this.strokeHasBeenWritten[k1][1]=0;
					}
				
				
					
				this.nextStroke=0;
						
				this.addShapes=new Array();
				
				this.addEventListener(MouseEvent.MOUSE_DOWN,mDown);
				this.addEventListener(MouseEvent.MOUSE_UP,mUp);
				if(this.mclkFlag==false){
					this.removeEventListener(MouseEvent.CLICK,mclk2);
					this.addEventListener(MouseEvent.CLICK,mclk,false,2);
					this.mclkFlag=true;
					
					}
				
			}
		}
	
	
	
	
	}